DISTINCT

This lesson demonstrates how to remove duplicates from a result set using the DISTINCT clause.

We'll cover the following

DISTINCT CLAUSE#

We’ll work with the DISTINCT clause in this lesson. This clause can be used to output unique rows in a result set. Remember DISTINCT is a post processing filter, meaning it is applied to the resulting rows of a query.

Example Syntax#

SELECT DISTINCT col1

FROM table;

Connect to the terminal below by clicking in the widget. Once connected, the command line prompt will show up. Enter or copy and paste the command ./DataJek/Lessons/21lesson.sh and wait for the MySQL prompt to start-up.

Terminal 1
Terminal

Click to Connect...

  1. Except for the primary key in our Actors table, every other column can have repeating values. For instance, the marital status is repeated amongst the Actors and if we wanted to see all the possible marital statuses in the table we could use the following query:

    SELECT DISTINCT MaritalStatus from Actors;
  1. The DISTINCT clause applies to all the selected columns in the output rows. For instance, if we add the column FirstName to the select query above, the result will consist of all the rows because each pair (MaritalStatus, FirstName) is unique. This outcome is exhibited below:

    SELECT DISTINCT MaritalStatus, FirstName from Actors;
Alias
Aggregate Methods
Mark as Completed
Report an Issue